<?php
// +----------------------------------------------------------------------
// | zlserver [ 小程序 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2018 http://www.zhongliansoft.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: tony <tanyu@zhongliansoft.com>
// +----------------------------------------------------------------------

namespace app\job\export;

use think\queue\Job;
use app\common\model\Ietask;
use think\facade\Log;


class bookselecttotal
{
    //执行导出任务
    public function exec(Job $job, $params)
    {
        $ietaskModle = new Ietask();
        $model = new \app\common\model\Common();
        $filter = json_decode($params['params'], true);
        // 参数解析
        $nd = $filter['year'];
        $bm = $filter['dept'];
        $zy = $filter['specialty'];
        $book_name = urldecode($filter['book_name']);
        $book_isbn = urldecode($filter['book_isbn']);
        $book_reward = urldecode($filter['book_reward']);
        $page = 0;
        $num = 100000;
        
        $list = $model->query("call proc_book_select_total('".$nd."','".$bm."','".$zy."','".$book_name."','".$book_isbn."','".$book_reward."','".$page."','".$num."');");
        $body = $list[0];
        
        $template = config('zlserver.bookselecttotal_export_templete');//使用模板
        $objPHPExcel = \PHPExcel_IOFactory::load($template);     //加载excel文件,设置模板 
        $objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);  //设置保存版本格式  
        
        //接下来就是写数据到表格里面去  
        $objActSheet = $objPHPExcel->getActiveSheet();
        $i = 2;  
        foreach ($body as $key => $val) {
            //这里是设置单元格的内容  
            $objActSheet->setCellValue('A'.$i, $val['bmmc']); 
            $objActSheet->setCellValue('B'.$i, $val['course_name']);
            $objActSheet->setCellValue('C'.$i, $val['zymc']);
            $objActSheet->setCellValue('D'.$i, $val['book_name']);
            $objActSheet->setCellValue('E'.$i, $val['book_isbn']);
            $objActSheet->setCellValue('F'.$i, $val['book_author']);
            $objActSheet->setCellValue('G'.$i, $val['book_concern']);
            $objActSheet->setCellValue('H'.$i, $val['num']);
            $i++;
        }
        
        $file_name = "bookselecttotal-xls-" . date("YmdHis", time()) . ".xls";
        $filePath  = defined("FILE_PATH") ? FILE_PATH : ROOT_PATH . 'public' . DS . 'static' . DS . 'file' . get_hash_dir($file_name);
        if (!is_dir($filePath)) {
            @mkdirs($filePath, 0777, true);
        } 
        $fullFilePath = $filePath . $file_name;
        // 1.保存至本地Excel表格  
        $objWriter->save($fullFilePath);
            
        $uData['file_name'] = $file_name;
        $uData['count'] = count($body);
        $uData['message'] = '导出成功';
        $uData['file_size'] = getRealSize(filesize($fullFilePath));
        $uData['file_path'] = $fullFilePath;
        $uData['status'] = $ietaskModle::EXPORT_SUCCESS_STATUS;
        $uData['utime'] = time();
        $ietaskModle->update($uData, ['id' => $params['task_id']]);
        
        $job->delete();
    }

    public function failed($data)
    {
        // ...任务达到最大重试次数后，失败了
    }
}